<?php
/**
 * Created by PhpStorm.
 * User: City
 * Date: 2018/3/7
 * Time: 13:34
 */

namespace Cms\Controller;


class RoleController extends BaseController
{
    public function _initialize() {
        //你可以在此覆盖父类方法
        parent::_initialize();
    }

    public function roleList(){
//设置面包导航，主加载器请配置
        $bread=array(
            '0'=>array(
                'name'=>'权限设置',
                'url'=>U('Cms/Role/#')
            ),
            '1'=>array(
                'name'=>'角色管理',
                'url'=>U('Cms/Role/roleList')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        $m=M('role',"basis_");
        $p=$_GET['p']?$_GET['p']:1;
        $search=I('search')?I('search'):'';
        if($search){
            $map['name']=array('like',"%$search%");
            $this->assign('search',$search);
        }
        $psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
        $cache=$m->where($map)->page($p,$psize)->select();
        $count=$m->where($map)->count();
        $this->getPage($count, $psize, 'Ystech-loader', '角色列表','Ystech-search');
        $this->assign('cache',$cache);
        $this->display();
    }

    public function roleDetail(){
        $bread=array(
            '0'=>array(
                'name'=>'权限设置',
                'url'=>U('Cms/Role/#')
            ),
            '1'=>array(
                'name'=>'角色管理',
                'url'=>U('Cms/Role/roleList')
            ),
            '2'=>array(
                'name'=>'角色编辑',
                'url'=>U('Cms/Role/roleDetail')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        $m=M('role',"basis_");
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            $roleMap['name'] = I('post.name');
            if($data[id]==''){
                $count = $m->where($roleMap)->count();
                if($count){
                    $info['status']=0;
                    $info['msg']='角色名称重复！';
                    $this->ajaxReturn($info);
                }
                unset($data['id']);
                $data['create_time'] = date("Y-m-d H:i:s");
                $re = $m->add($data);
                if($re){
                    $info['status']=1;
                    $info['msg']='创建成功！';
                    $this->ajaxReturn($info);
                }else{
                    $info['status']=0;
                    $info['msg']='创建失败！';
                    $this->ajaxReturn($info);
                }
            }else{
                $data['update_time'] = date("Y-m-d H:i:s");
                $re = $m->save($data);
                if($re){
                    $info['status']=1;
                    $info['msg']='编辑成功！';
                    $this->ajaxReturn($info);
                }else{
                    $info['status']=0;
                    $info['msg']='编辑失败！';
                    $this->ajaxReturn($info);
                }
            }
        }
        $map['id'] = $id;
        $roleDetail = $m->where($map)->find();
        $this->assign('cache',$roleDetail);
        $this->display();
    }

    public function embarRole(){
        $id=$_GET['id'];
        $m=M('role',"basis_");
        if(!$id){
            $info['status']=0;
            $info['msg']='ID不能为空!';
            $this->ajaxReturn($info);
        }
        $ids = explode(",",$id);
        $re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>1));
        if($re){
            $info['status']=1;
            $info['msg']='删除成功!';
        }else{
            $info['status']=0;
            $info['msg']='删除失败!';
        }
        $this->ajaxReturn($info);
    }

    public function enabledRole(){
        $id=$_GET['id'];
        $m=M('role',"basis_");
        if(!$id){
            $info['status']=0;
            $info['msg']='ID不能为空!';
            $this->ajaxReturn($info);
        }
        $ids = explode(",",$id);
        $re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>0));
        if($re){
            $info['status']=1;
            $info['msg']='启用成功!';
        }else{
            $info['status']=0;
            $info['msg']='启用失败!';
        }
        $this->ajaxReturn($info);
    }

    public function nodeList(){
        //设置面包导航，主加载器请配置
        $bread=array(
            '0'=>array(
                'name'=>'权限设置',
                'url'=>U('Cms/Role/#')
            ),
            '1'=>array(
                'name'=>'功能管理',
                'url'=>U('Cms/Role/nodeList')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        $nodeModel = M('node','basis_');
        $role=M('role',"basis_");
        $roleList = $role->where('is_del=0')->select();
        $nodeId = I('node');
        $roleId = I('role');
        foreach($roleList as $key=>$item){
            if($item['id']==$roleId){
                $roleList[$key]['select'] = '1';
            }
        }
        $nodeList = array();
        $firstNode = $nodeModel->where('pid=0')->order('sort desc')->select();
        if($nodeId!=''){
            $nodeList[] = $nodeModel
                ->field('basis_node.*,basis_node_type.type_name')
                ->join('basis_node_type on basis_node_type.id=basis_node.type_id')
                ->where('id='.$nodeId)
                ->find();
            $nextNode = $nodeModel
                ->field('basis_node.*,basis_node_type.type_name')
                ->join('basis_node_type on basis_node_type.id=basis_node.type_id')
                ->where('pid='.$nodeId)
                ->select();
            if($nextNode){
                $nodeList = array_merge($nextNode,$nodeList);
            }
            foreach($nextNode as $item){
                $lastList = $nodeModel
                    ->field('basis_node.*,basis_node_type.type_name')
                    ->join('basis_node_type on basis_node_type.id=basis_node.type_id')
                    ->where('pid='.$item['id'])
                    ->select();
                if($lastList){
                    $nodeList = array_merge($nodeList,$lastList);
                }
            }
            foreach($firstNode as $key=>$item){
                if($item['id']==$nodeId){
                    $firstNode[$key]['select'] = '1';
                }
            }
        }else{
            $nodeList = $nodeModel
                ->field('basis_node.*,basis_node_type.type_name')
                ->join('basis_node_type on basis_node_type.id=basis_node.type_id')
                ->select();
        }
        $nodeList = list_to_tree($nodeList);
        $this->assign('firstNode',$firstNode);
        $this->assign('nodeList',$nodeList);
        $this->assign('roleList',$roleList);
        $this->display();
    }
    public function embarNode(){
        $id=$_GET['id'];
        $m=M('node',"basis_");
        if(!$id){
            $info['status']=0;
            $info['msg']='ID不能为空!';
            $this->ajaxReturn($info);
        }
        $ids = explode(",",$id);
        $re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>1));
        if($re){
            $info['status']=1;
            $info['msg']='删除成功!';
        }else{
            $info['status']=0;
            $info['msg']='删除失败!';
        }
        $this->ajaxReturn($info);
    }

    public function enabledNode(){
        $id=$_GET['id'];
        $m=M('node',"basis_");
        if(!$id){
            $info['status']=0;
            $info['msg']='ID不能为空!';
            $this->ajaxReturn($info);
        }
        $ids = explode(",",$id);
        $re=$m->where(array('id'=>array('in',$ids)))->save(array('is_del'=>0));
        if($re){
            $info['status']=1;
            $info['msg']='启用成功!';
        }else{
            $info['status']=0;
            $info['msg']='启用失败!';
        }
        $this->ajaxReturn($info);
    }

    public function nodeDetail(){
        $bread=array(
            '0'=>array(
                'name'=>'权限设置',
                'url'=>U('Cms/Role/#')
            ),
            '1'=>array(
                'name'=>'功能管理',
                'url'=>U('Cms/Role/roleList')
            ),
            '2'=>array(
                'name'=>'功能编辑',
                'url'=>U('Cms/Role/roleDetail')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        $m=M('node',"basis_");
        $id = I('id');
        if(IS_POST){
            $data = I('post.');
            $roleMap['name'] = I('post.name');
            if($data[id]==''){
                $count = $m->where($roleMap)->count();
                if($count){
                    $info['status']=0;
                    $info['msg']='功能名称重复！';
                    $this->ajaxReturn($info);
                }
                unset($data['id']);
                $re = $m->add($data);
                if($re){
                    $info['status']=1;
                    $info['msg']='创建成功！';
                    $this->ajaxReturn($info);
                }else{
                    $info['status']=0;
                    $info['msg']='创建失败！';
                    $this->ajaxReturn($info);
                }
            }else{
                $re = $m->save($data);
                if($re){
                    $info['status']=1;
                    $info['msg']='编辑成功！';
                    $this->ajaxReturn($info);
                }else{
                    $info['status']=0;
                    $info['msg']='编辑失败！';
                    $this->ajaxReturn($info);
                }
            }
        }
        $pid = I('pid');
        $map['id'] = $id;
        $roleDetail = $m->where($map)->find();
        $nodeType = M('node_type','basis_')->select();
        if(!empty($pid)){
            $typeId = $m->where(array('id'=>$pid))->field('type_id')->find();
            $this->assign('typeId',$typeId);
        }
        $this->assign('pid',$pid);
        $this->assign('nodeType',$nodeType);
        $this->assign('cache',$roleDetail);
        $this->display();

    }

    public function getRoleNodeInfo(){
        $role_id = $_POST["role_id"];
        $m = M("role_node","basis_");
        $map['role_id'] = $role_id;
        $data = $m->where($map)->select();
        if ($data !== false) {// 读取成功
            $return['data'] = $data;
            $return['status'] = 1;
            $this -> ajaxReturn($return);
        }
    }

    //绑定用户组管理的功能

    public function bindNode(){
        $role_id = $_POST['role_id'];
        $node_id = $_POST['node_id'];
        $newNodeList = $_POST['node_list'];
        $m = M("role_node","basis_");
        $map['role_id'] = $role_id;
        if(empty($role_id)){
            $info['status']=0;
            $info['msg']='无可用用户组！';
            $this->ajaxReturn($info);
        }
        if($node_id!=0){
            $nodeModel = M('node','basis_');
            $nodeList[] = $nodeModel->where('id='.$node_id)->find();
            $nextNode = $nodeModel->where('pid='.$node_id)->order('sort desc')->select();
            if($nextNode){
                $nodeList = array_merge($nextNode,$nodeList);
            }
            foreach($nextNode as $item){
                $lastList = $nodeModel->order('sort desc')->where('pid='.$item['id'])->select();
                if($lastList){
                    $nodeList = array_merge($nodeList,$lastList);
                }
            }
            $node_ids = array();
            foreach($nodeList as $key=>$item){
                $node_ids[] = $item['id'];
            }
            $map['node_id']=array('in',$node_ids);
            $m->where($map)->delete();
        }else{
            $m->where(array('role_id'=>$role_id))->delete();
        }
        $m->addAll($newNodeList);
        $info['status']=1;
        $info['msg']='保存成功！';
        $this->ajaxReturn($info);

    }

    //绑定用户权限组

    public function bindRole(){
        $bread=array(
            '0'=>array(
                'name'=>'权限设置',
                'url'=>U('Cms/Role/#')
            ),
            '1'=>array(
                'name'=>'权限分配',
                'url'=>U('Cms/Role/roleList')
            )
        );
        $this->assign('breadhtml',$this->getBread($bread));
        $m=M('user',"ystech_");
        $role=M('role',"basis_");
        //$roleList = $role->select();
        $roleList = $role->where('is_del=0')->field('id,name')->getField('id,name');
        $p=$_GET['p']?$_GET['p']:1;
        $search=I('search')?I('search'):'';
        if($search){
            $map['username']=array('like',"%$search%");
            $this->assign('search',$search);
        }
        $psize=self::$CMS['set']['pagesize']?self::$CMS['set']['pagesize']:20;
        $cache=$m->field('ystech_user.id,ystech_user.username,basis_role.name as role_name')
            ->join('basis_role_user on basis_role_user.user_id=ystech_user.id')
            ->join('basis_role on basis_role.id = basis_role_user.role_id')
            ->where($map)
            ->page($p,$psize)
            ->select();
        $count=$m->where($map)->count();
        $this->getPage($count, $psize, 'Ystech-loader', '用户列表','Ystech-search');
        $this->assign('cache',$cache);
        $this->assign('roleList',$roleList);
        $this->display();
    }

    public function editBindRole(){
        $userIds = $_POST['user_ids'];
        $roleId = $_POST['role_id'];
        $role=M('role',"basis_");
        $roleList = $role->select();
        $roleIds = array();
        foreach($roleList as $item){
            $roleIds[] = $item['id'];
        }
        if(!in_array($roleId,$roleIds)){
            $info['status']=0;
            $info['msg']='无效用户组！';
            $this->ajaxReturn($info);
        }
        $roleUser = M('role_user','basis_');
        $map['user_id'] = array('in',$userIds);
        $data['role_id'] = $roleId;
        $roleUser->where($map)->save($data);
        $info['status']=1;
        $info['msg']='编辑成功！';
        $this->ajaxReturn($info);
    }
}
